//
//  StudentListViewController.m
//  PCSchool
//
//  Created by Arindam Ghosh on 7/12/13.
//  Copyright (c) 2013 Jayanta. All rights reserved.
//

#import "StudentListViewController.h"

@interface StudentListViewController ()

@end

@implementation StudentListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"SDD");
    // Do any additional setup after loading the view from its nib.
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%@",NSStringFromClass(self.class));
    
    [self createLoadingView];
    [self.appDelegate.dataModel sendRequestFor:CLOSEST_STUDENT_DETAILS from:self];

    
    
    self.studentArray = [NSMutableArray array];
    
    /*[self.studentArray addObject:@"Adam 2"];
    [self.studentArray addObject:@"Adam 3"];
    [self.studentArray addObject:@"Adam 4"];
    [self.studentArray addObject:@"Adam 5"];
    [self.studentArray addObject:@"Adam 6"];
    [self.studentArray addObject:@"Adam 7"];
    [self.studentArray addObject:@"Adam 8"];
    [self.studentArray addObject:@"Adam 9"];
    [self.studentArray addObject:@"Adam 10"];
    [self.studentArray addObject:@"Adam 11"];
    [self.studentArray addObject:@"Adam 12"];
    [self.studentArray addObject:@"Adam 13"];
    [self.studentArray addObject:@"Adam 14"];
    [self.studentArray addObject:@"Adam 15"];*/
    
    
    
    self.studentDetailsArray4Leftpart = [[NSMutableArray alloc] initWithObjects:@"SurName",
                                         @"KnownAsName",
                                         @"Email",
                                         @"Home Teacher",
                                         @"Home Room",
                                         @"House",
                                         @"Dean",
                                         @"StudentAlert", nil];
    
    self.studentDetailsDictionary = [NSMutableDictionary dictionary];

    

    self.rightPanelTableView.scrollEnabled = NO;
    
    self.rightPanelStudentImageView.layer.cornerRadius = 4.0;
    self.rightPanelStudentImageView.layer.borderWidth = 1.0;
    self.rightPanelStudentImageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    
    self.rightPanelStudentImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.rightPanelGraphView.contentMode = UIViewContentModeScaleAspectFit;
    
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    @try {
        
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.leftPanelTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
    }
    @catch (NSException *exception) {
        NSLog(@"EXC %@",exception);
    }
    @finally {
        
    }
   
}


#pragma mark - TableView Delegates & Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        return 46.0;
    }
    if (tableView.tag == 102) {
        
        return 50.0;
    }
    return 0.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        [self setStudentData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 101) {
        return [self.studentArray count];

    }
    if (tableView.tag == 102) {
        return [self.studentDetailsArray4Leftpart count];
        
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        static NSString *CellIdentifier = @"StudenListCell";
        StudenListCell * cell =(StudenListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(StudenListCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            
        }
        
        [self configureCell:cell atIndexPath:indexPath];
        
        return cell;
    }
    if (tableView.tag == 102) {
        
        static NSString *CellIdentifier = @"StudentDetailsCell";
        StudentDetailsCell * cell =(StudentDetailsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(StudentDetailsCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self configureCell2:cell atIndexPath:indexPath];
        
        return cell;
    }
    
    return nil;
    
}

-(void)configureCell:(StudenListCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellTextLabel.text = [self.studentArray objectAtIndex:indexPath.row];
    [cell.personImageView setImageWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@Students/GetStudentImageByName/%@",BASE_URL,[self.appDelegate.arrayForStudentDetails valueForKey:@"MemberCode"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"no-imageBig.png"]];
}

-(void)configureCell2:(StudentDetailsCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.leftLabel.text = [self.studentDetailsArray4Leftpart objectAtIndex:indexPath.row];
    cell.rightLabel.text = [self.studentDetailsDictionary valueForKey:cell.leftLabel.text];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark postData
-(id)postData
{
    NSLog(@"%@",self.appDelegate.arrayForStudentDetails);
	NSMutableDictionary *aDict=[NSMutableDictionary dictionary];
    [aDict setValue:[self.appDelegate.arrayForStudentDetails valueForKey:@"MemberId"] forKey:UPDATED_USERID];
    [aDict setValue:[self.appDelegate.arrayForStudentDetails valueForKey:@"MemberCode"] forKey:MEMBER_CODE];

    return aDict;
}

-(void) notifyRequesterWithData:(id)aManager
{
    [self removeLoadingView];
    
    ConnectionManager* aCon = aManager;
    NSLog(@"RESPONSE DATA  %@",aCon.responseString);
    
    if ([aCon.aRequest error])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:[[aCon.aRequest error] localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    else
    {
        if ([aCon.requestName isEqualToString:CLOSEST_STUDENT_DETAILS])
        {
            
            self.appDelegate.closestStudentDetails = [aCon.responseString objectFromJSONString];
            
            NSLog(@">>>>>>>>>  %@",self.appDelegate.closestStudentDetails);

        }
        
        
    }
    
    [self.studentArray addObject:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"FullName"]];

    [self setStudentData];

    
}


#pragma mark - Set Student data

-(void) setStudentData
{
    NSLog(@"DATA >>>>>>  %@ %@",[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"SurName"],[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"UDID"]);
    
    [self.studentDetailsDictionary removeAllObjects];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"SurName"] forKey:@"SurName"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"KnownAsName"] forKey:@"KnownAsName"];
    
    [self.studentDetailsDictionary setValue:[[[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"StudentContacts"] objectAtIndex:3] valueForKey:@"Email"] forKey:@"Email"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"HomeTeacher"] forKey:@"Home Teacher"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"HomeRoom"] forKey:@"Home Room"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"House"] forKey:@"House"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"Dean"] forKey:@"Dean"];
    
    [self.studentDetailsDictionary setValue:[[self.appDelegate.closestStudentDetails objectAtIndex:0] valueForKey:@"StudentAlert"] forKey:@"StudentAlert"];
    
    [self.rightPanelStudentImageView setImageWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@Students/GetStudentImageByName/%@",BASE_URL,[self.appDelegate.arrayForStudentDetails valueForKey:@"MemberCode"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"no-imageBig.png"]];
    
    [self.rightPanelTableView reloadData];
    [self.leftPanelTableView reloadData];

}


#pragma mark - Button Action

- (IBAction)proximityButtonAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    
    int selectedIndex = btn.selected ? 0 : 1;
        
    btn.selected = selectedIndex;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_baseImageView release];
    [_rightPanelImageView release];
    [_leftPanelView release];
    [_leftPanelImageView release];
    [_leftPanelTableView release];
    [_searchBarImgView release];
    [_searchBarTextField release];
    [_proximityButton release];
    [_searchBarLeftimageView release];
    [_rightPanelTableView release];
    [_rightPanelheaderImageView release];
    [_studentDetailsDictionary release];
    [_rightPanelStudentImageView release];
    [_rightPanelGraphView release];
    [_studentDetailsArray4Leftpart release];
    [super dealloc];
}

@end
